package com.hp.dao;

import com.hp.entity.MedicalAdvice;
import com.hp.entity.Patient;
import com.hp.entity.dto.MedicalAdviceDto;
import com.hp.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * SELECT m.id,m.registry_id AS registryId,m.user_id AS userId,m.chief_complaint AS chiefComplaint,
 * m.diagnosis AS diagnosis,m.created_time AS createdTime,m.updated_time AS updatedTime,
 * u.id AS userId,u.user_name AS userName FROM medical_advice AS m
 * LEFT JOIN sys_users AS u
 * ON m.user_id=u.id
 * WHERE m.`registry_id`=21
 */
public class MedicalAdviceDao {
    public MedicalAdviceDto getMedicalAdviceByRegistryId(int registryId) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="SELECT m.id,m.registry_id AS registryId,m.user_id AS userId,m.chief_complaint AS chiefComplaint," +
                "m.diagnosis AS diagnosis,m.created_time AS createdTime,m.updated_time AS updatedTime," +
                "u.id AS userId,u.user_name AS userName FROM medical_advice AS m " +
                "LEFT JOIN sys_users AS u " +
                "ON m.user_id=u.id " +
                "WHERE m.registry_id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<MedicalAdviceDto>(MedicalAdviceDto.class),registryId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean updateMedicalAdvice(MedicalAdvice medicalAdvice) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="update medical_advice set registry_id=?,user_id=?,chief_complaint=?,diagnosis=?,updated_time=? where id=?";
        try {
            return queryRunner.update(sql,
                    medicalAdvice.getRegistryId(),
                    medicalAdvice.getUserId(),
                    medicalAdvice.getChiefComplaint(),
                    medicalAdvice.getDiagnosis(),
                    medicalAdvice.getUpdatedTime(),
                    medicalAdvice.getId()
            )>0?true:false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean insertMedicalAdvice(MedicalAdvice medicalAdvice) {
        QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource());
        String sql="insert into medical_advice(id,registry_id,user_id,chief_complaint,diagnosis," +
                "updated_time,created_time) values(null,?,?,?,?,?,?)";
        try {
            return queryRunner.update(
                    sql,
                    medicalAdvice.getRegistryId(),
                    medicalAdvice.getUserId(),
                    medicalAdvice.getChiefComplaint(),
                    medicalAdvice.getDiagnosis(),
                    medicalAdvice.getUpdatedTime(),
                    medicalAdvice.getCreatedTime()
            )>0?true:false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
